package deng.qiya.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import deng.qiya.model.CardType;
import deng.qiya.util.CustomerContextHolder;
import deng.qiya.wechat.Constant;

public class CardTypeOper {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Cacheable("allCardType")
	public Map<String,CardType> getAllCardType(){
		String sqlString = "select Cd, Descript ,Balance from crs_help where FldNm ='cardtp'";
		CustomerContextHolder.setCustomerType(Constant.dscrs);
		List<CardType> list = jdbcTemplate.query(sqlString, new CardTypeMapper());
		Map<String,CardType> map = new HashMap<String, CardType>();
		for(CardType ct: list){
			map.put(ct.getCd(), ct);
		}
		
		return map;
	}
	
	class CardTypeMapper implements RowMapper<CardType>{

		@Override
		public CardType mapRow(ResultSet rs, int rowNum) throws SQLException {
			CardType ct = new CardType();
			ct.setCd(rs.getString("Cd"));
			ct.setDescription(rs.getString("Descript"));
			ct.setBalance(rs.getString("Balance"));
			return ct;
		}
		
	}
}
